package GameData;

import Algorithm.Pathfinding;
import GameLogic.Tile;
import java.awt.Color;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import javax.imageio.ImageIO;

public class Map 
{
    private static boolean[][] Map;
    private static int w, h;
    private static Point e;
    public static Tile[][] LoadFromFile(String path, Point start, Point end, ArrayList<Point> Waypoint)
    {
        try 
        {
            BufferedImage img = ImageIO.read(new File(path));
            w = img.getWidth(null);
            h = img.getHeight(null);
            boolean hasEnd = false;
            Tile[][] ret = new Tile[w][h];            
            //System.out.println("img color: " + (img.getRGB(70, 77) == Color.RED.getRGB()));
            for(int y = 0; y < h; y++)
                for(int x = 0; x < w; x++)
                {
                    int c = img.getRGB(x, y);
                    
                    if(c == Color.BLUE.getRGB())
                    {
                        Waypoint.add(new Point(x, y));
                        ret[x][y] = Tile.WALKABLE;
                        continue;
                    }
                    
                    if(!hasEnd && c == Color.RED.getRGB())
                    {
                        end.x = x;
                        end.y = y;
                        hasEnd = true;
                        ret[x][y] = Tile.WALKABLE;
                        continue;
                    }
                    
                    if(c == Color.BLACK.getRGB())
                        ret[x][y] = Tile.NONE;
                    else if (c == Color.WHITE.getRGB())
                        ret[x][y] = Tile.WALKABLE;
                    else if (c == Color.YELLOW.getRGB())
                        ret[x][y] = Tile.BUILDABLE;
                    else if(c == Color.GREEN.getRGB())
                        ret[x][y] = Tile.BOTH;
                    else
                        return null;
                }
            if(!hasEnd || Waypoint.isEmpty())
                return null;
            e = end;
            Map = new boolean[w][h];
            for(int x = 0; x < w; x++)
                for(int y = 0; y <h; y++)
                    Map[x][y] = !ret[x][y].canWalk;
            Collections.sort(Waypoint, new Comparator<Point>()
            {
                @Override
                public int compare(Point o1, Point o2) {
                    double d1 = Pathfinding.GetRouteLen(new Point(w, h), Map, o1, e),
                           d2 = Pathfinding.GetRouteLen(new Point(w, h), Map, o2, e);
                    if(d1 > d2)
                        return -1;
                    return 1;
                }
            });
            Waypoint.add(end);
            Point s = Waypoint.remove(0);
            start.x = s.x;
            start.y = s.y;
            return ret;
        } 
        catch (Exception e) 
        {
            return null;
        }
    }
}
